package com.smartfast4j.manager.modules.sys.service.impl;


import com.smartfast4j.common.Constants;
import com.smartfast4j.common.bean.R;
import com.smartfast4j.common.exception.Smartfast4jException;
import com.smartfast4j.manager.modules.sys.service.SysDictService;
import com.smartfast4j.mapper.sys.SysDictMapper;
import com.smartfast4j.pojo.sys.SysDict;
import com.smartfast4j.pojo.sys.SysDictExample;
import org.apache.commons.collections.CollectionUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class SysDictServiceImpl implements SysDictService {
    @Autowired
    private SysDictMapper sysDictMapper;

    private static final Logger logger = LoggerFactory.getLogger(SysDictServiceImpl.class);

    public int countByExample(SysDictExample example) {
        int count = this.sysDictMapper.countByExample(example);
        logger.debug("count: {}", count);
        return count;
    }

    public SysDict selectByPrimaryKey(Long id) {
        return this.sysDictMapper.selectByPrimaryKey(id);
    }

    public List<SysDict> selectByExample(SysDictExample example) {
        return this.sysDictMapper.selectByExample(example);
    }

    public int deleteByPrimaryKey(Long id) {
        return this.sysDictMapper.deleteByPrimaryKey(id);
    }

    public int updateByPrimaryKeySelective(SysDict record) {
        return this.sysDictMapper.updateByPrimaryKeySelective(record);
    }

    public int updateByPrimaryKey(SysDict record) {
        return this.sysDictMapper.updateByPrimaryKey(record);
    }

    public int deleteByExample(SysDictExample example) {
        return this.sysDictMapper.deleteByExample(example);
    }

    public int updateByExampleSelective(SysDict record, SysDictExample example) {
        return this.sysDictMapper.updateByExampleSelective(record, example);
    }

    public int updateByExample(SysDict record, SysDictExample example) {
        return this.sysDictMapper.updateByExample(record, example);
    }

    public int insert(SysDict record) {
        return this.sysDictMapper.insert(record);
    }

    public int insertSelective(SysDict record) {
        return this.sysDictMapper.insertSelective(record);
    }

    public int insertBatchSelective(List<SysDict> records) {
        return this.sysDictMapper.insertBatchSelective(records);
    }

    public int updateBatchByPrimaryKeySelective(List<SysDict> records) {
        return this.sysDictMapper.updateBatchByPrimaryKeySelective(records);
    }

    @Override
    public void save(SysDict record) {
        if(record.getId() != null){
            //更新
            SysDictExample dictExample = new SysDictExample();
            dictExample.createCriteria().andDeleteFlagEqualTo(Constants.DELETE_FLAG.NO).andDictNameEqualTo(record.getDictName());
            List<SysDict> sysDictList = selectByExample(dictExample);
            if(CollectionUtils.isNotEmpty(sysDictList)){
                SysDict dict = sysDictList.get(0);
                //修改的是同一条记录
                if(dict.getId().equals(record.getId())){
                    record.setUpdateTime(new Date());
                    updateByPrimaryKeySelective(record);
                }else{
                    //根据字典名称找到了相同的记录，表示字典名称和别的字典重名
                    throw new Smartfast4jException("字典名称已经存在，请换一个名称");
                }
            }else{
                record.setUpdateTime(new Date());
                updateByPrimaryKeySelective(record);
            }
        }else{
            //新增
            SysDictExample dictExample = new SysDictExample();
            dictExample.createCriteria().andDeleteFlagEqualTo(Constants.DELETE_FLAG.NO).andDictNameEqualTo(record.getDictName());
            int count = countByExample(dictExample);
            if(count>0){
                throw new Smartfast4jException("字典名称已经存在，请换一个名称");
            }
            record.setCreateTime(new Date());
            record.setDeleteFlag(Constants.DELETE_FLAG.NO);
            insertSelective(record);
        }
    }

    @Override
    public void delete(List<Long> idList) {
        //根据字典ID删除字典(逻辑删除)
        SysDictExample dictExample = new SysDictExample();
        dictExample.createCriteria().andIdIn(idList);
        SysDict sysDict = new SysDict();
        sysDict.setUpdateTime(new Date());
        sysDict.setDeleteFlag(Constants.DELETE_FLAG.YES);
        updateByExampleSelective(sysDict,dictExample);
    }
}